package com.findtech.threePomelos.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.findtech.threePomelos.R;
import com.findtech.threePomelos.base.MyActionBarActivity;
import com.findtech.threePomelos.base.MyApplication;
import com.findtech.threePomelos.entity.BabyInfoEntity;
import com.findtech.threePomelos.net.NetWorkRequest;
import com.findtech.threePomelos.utils.MyCalendar;
import com.findtech.threePomelos.utils.Tools;
import com.findtech.threePomelos.view.dialog.CustomDialog;

import java.text.ParseException;

/**
 * Created by zhi.zhang on 5/3/16.
 */
public class BabyHealthTipsItemsActivity extends MyActionBarActivity {

    private BabyInfoEntity babyInfoEntity = BabyInfoEntity.getInstance();
    private TextView tipsBabyBirthday, tipsBabyBirthdayInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_baby_health_tip_item);
        MyApplication.getInstance().addActivity(this);
        setToolbar(getResources().getString(R.string.title_activity_baby_health_tip), true);
        tipsBabyBirthday = (TextView) findViewById(R.id.tips_baby_birthday);
        tipsBabyBirthdayInfo = (TextView) findViewById(R.id.tips_baby_birthday_info);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        int index = babyInfoEntity.getBirthday().indexOf("年");
        tipsBabyBirthday.setText(babyInfoEntity.getBirthday().substring(index + 1, babyInfoEntity.getBirthday().length()));
        try {
            final String currentDate = Tools.getSystemTimeInChina("yyyy-MM-dd");
            String birthday = babyInfoEntity.getBirthday().replace("年", "-").replace("月", "-").replace("日", "");
            babyInfoEntity.setBabyTotalDay(this, birthday, "0");
            MyCalendar myCalendar = new MyCalendar(birthday, currentDate);
            tipsBabyBirthdayInfo.setText(String.format(getString(R.string.text_age_description_text), babyInfoEntity.getBabySex(), myCalendar.getDate()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public void onClickCardView(View view) {
        if (TextUtils.isEmpty(babyInfoEntity.getBirthday())) {
            gotoBabyInfoViewDialog();
            return;
        }
        Intent intent = new Intent(this, BabyHealthTipsContentActivity.class);
        int id = view.getId();
        switch (id) {
            case R.id.tip_item_childcare_points://育儿要点
                intent.putExtra(Tools.TIPS_TYPE, NetWorkRequest.FILE_CHILDCARE_POINTS);
                intent.putExtra(Tools.TIPS_TYPE_TITLE, getResources().getString(R.string.title_activity_childcare_points));
                startActivity(intent);
                break;
            case R.id.tip_item_dp_sign://发育体征
                intent.putExtra(Tools.TIPS_TYPE, NetWorkRequest.FILE_DEVELOPMENT_SIGNS);
                intent.putExtra(Tools.TIPS_TYPE_TITLE, getResources().getString(R.string.title_activity_dp_sign));
                startActivity(intent);
                break;
            case R.id.tip_item_science_tip://科普提示
                intent.putExtra(Tools.TIPS_TYPE, NetWorkRequest.FILE_SCIENCE_TIP);
                intent.putExtra(Tools.TIPS_TYPE_TITLE, getResources().getString(R.string.title_activity_science_tip));
                startActivity(intent);
                break;
            case R.id.tip_item_grown_concern://成长关注
                intent.putExtra(Tools.TIPS_TYPE, NetWorkRequest.FILE_GROWN_CONCERN);
                intent.putExtra(Tools.TIPS_TYPE_TITLE, getResources().getString(R.string.title_activity_grown_concern));
                startActivity(intent);
                break;
            case R.id.tip_item_parental_interaction://亲子互动
                intent.putExtra(Tools.TIPS_TYPE, NetWorkRequest.FILE_PARENTAL);
                intent.putExtra(Tools.TIPS_TYPE_TITLE, getResources().getString(R.string.title_activity_parental_interaction));
                startActivity(intent);
                break;
            default:
                break;
        }
    }


    /**
     * 设置宝贝生日
     */
    private void gotoBabyInfoViewDialog() {
        final CustomDialog.Builder builder = new CustomDialog.Builder(this);
        builder.setTitle("提示");
        builder.setNotifyInfo("请输入宝贝的生日，才可以提供更好的育儿建议哦！");

        builder.setShowButton(true);
        builder.setPositiveButton("去设置", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                startActivity(new Intent(BabyHealthTipsItemsActivity.this, BabyInfoActivity.class));
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }
}
